package org.spring.springboot.app.community.structureic.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.spring.springboot.app.base.R;
import org.spring.springboot.app.community.info.domain.vo.CommunityInfoInsertAndUpadteVO;
import org.spring.springboot.app.community.structureic.domain.po.HardIcPO;
import org.spring.springboot.app.community.structureic.domain.vo.HardPositionVO;
import org.spring.springboot.app.community.structureic.service.ICommunityStructureIcService;
import org.spring.springboot.app.community.structureic.service.IHardIcService;
import org.spring.springboot.app.sys.dict.domain.po.SysDictPO;
import org.spring.springboot.app.sys.dict.domain.vo.SysDictVO;
import org.spring.springboot.component.annotation.AccessTokenVerify;
import org.spring.springboot.util.ApiIndex;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Api(description="社区-IC卡管理操作接口",tags = ApiIndex.CommunityStructureIcController)
@RequestMapping(value = "/api/communityStructureIc")
@RestController
public class CommunityStructureIcController {

    @Autowired
    private ICommunityStructureIcService communityStructureIcService;

    @Autowired
    private IHardIcService hardIcService;

    @ApiOperation(value = "查询小区所有IC卡-分页查询")
    @RequestMapping(value = "/findPage", method = RequestMethod.GET)
    public R<PageInfo<List<HardIcPO>>> findPage(
            @ApiParam(value = "小区ID", required = false) @RequestParam(defaultValue = "") String infoId,
            @ApiParam(value = "IC卡号", required = false) @RequestParam(defaultValue = "") String hardEquiId,
            @ApiParam(value = "页数", required = true) @RequestParam(defaultValue = "1") int pageNum,
            @ApiParam(value = "每页条数", required = true) @RequestParam(defaultValue = "10") int pageSize
    ) {
        R<PageInfo<List<HardIcPO>>> result = new R<PageInfo<List<HardIcPO>>>();
        List<HardIcPO> _list = hardIcService.findPage(pageNum, pageSize, infoId,hardEquiId);
        PageInfo p =new PageInfo(_list);
        p.setList(p.getList());
        result.setData(p);
        return result;
    }
    @ApiOperation(value = "查询组织机构绑定IC卡-分页查询")
    @RequestMapping(value = "/findPageById", method = RequestMethod.GET)
    public R<PageInfo<List<HardPositionVO>>> findPageById(
            @ApiParam(value = "组织机构ID", required = true) @RequestParam(defaultValue = "") String structureId,
            @ApiParam(value = "IC卡号", required = false) @RequestParam(defaultValue = "") String hardEquiId,
            @ApiParam(value = "页数", required = true) @RequestParam(defaultValue = "1") int pageNum,
            @ApiParam(value = "每页条数", required = true) @RequestParam(defaultValue = "10") int pageSize
    ) {
        R<PageInfo<List<HardPositionVO>>> result = new R<PageInfo<List<HardPositionVO>>>();
        List<HardPositionVO> _list = hardIcService.findPageById(pageNum, pageSize, structureId,hardEquiId);
        PageInfo p =new PageInfo(_list);
        p.setList(p.getList());
        result.setData(p);
        return result;
    }

    @ApiOperation(value = "查询IC卡绑定位置")
    @RequestMapping(value = "/findBinding", method = RequestMethod.GET)
    public R<List<HardPositionVO>> findBinding(
            @ApiParam(value = "IC卡号", required = true) @RequestParam(defaultValue = "") String hardEquiId,
            @ApiParam(value = "小区ID", required = true) @RequestParam(defaultValue = "") String infoId
    ) {
        R<List<HardPositionVO>> result = new R<List<HardPositionVO>>();
        List<HardPositionVO> _list = hardIcService.findBinding(hardEquiId,infoId);
        result.setData(_list);
        return result;
    }

    @ApiOperation(value = "上传IC卡数据")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public R uploadIcCard(
            @ApiParam(value = "文件", required = true) @RequestParam("file") MultipartFile file,
            @ApiParam(value = "组织机构id(小区，楼栋，楼层，楼栋)", required = true) @RequestParam("id") String id
    ) {
        R result = new R();
        int count=hardIcService.save(id,file);
        if(count==1){
            result.setCode(1);
            result.setMessage("上传文件必须为(.txt)文本文件");
            return result;
        }
        if(count==2) {
            result.setMessage("请上传有 result.setCode(1);效的，组织机构ID。");
            return result;
        }
        if(count==3) {
            result.setMessage("上传的IC卡数量必须小于45000条");
            return result;
        }
        return  result;
    }
    @ApiOperation(value = "删除IC卡关联")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "/delRelate", method = RequestMethod.DELETE)
    public R delRelate(
            @ApiParam(value = "组织机构ID", required = true) @RequestParam("id") String id,
            @ApiParam(value = "IC卡号", required = true) @RequestParam("hardEquiId") String hardEquiId
    ) {
        R result = new R();
        if(id==null ||id.length()<1){
            result.setCode(1);
            result.setMessage("id不能为空");
            return result;
        }
        if(hardEquiId==null ||hardEquiId.length()<1){
            result.setCode(1);
            result.setMessage("hardEquiId不能为空");
            return result;
        }
        int count=hardIcService.deleteRelate(id,hardEquiId);
        if(count<1){
            result.setCode(1);
            result.setMessage("删除失败");
        }
        return  result;
    }
    @ApiOperation(value = "删除小区IC卡及以下绑定数据")
    @ApiImplicitParam(name = "token", value = "token", paramType = "query", dataType = "String", required = true)
    @AccessTokenVerify
    @RequestMapping(value = "/del", method = RequestMethod.DELETE)
    public R delete(
            @ApiParam(value = "小区ID", required = true) @RequestParam("id") String id,
            @ApiParam(value = "IC卡号", required = true) @RequestParam("hardEquiId") String hardEquiId
    ) {
        R result = new R();
        if(id==null ||id.length()<1){
            result.setCode(1);
            result.setMessage("id不能为空");
            return result;
        }
        if(hardEquiId==null ||hardEquiId.length()<1){
            result.setCode(1);
            result.setMessage("hardEquiId不能为空");
            return result;
        }
        int count=hardIcService.delete(id,hardEquiId);
        if(count<1){
            result.setCode(1);
            result.setMessage("删除失败");
        }
        return  result;
    }
}
